//
//  ViewController.m
//  AlipayDemo
//
//  Created by apple on 15/12/28.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "ViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "DataSigner.h"


#define kServerPath @"http://218.244.149.155:8080/"  //客户端服务器
#define kInterfacePath @"specialVehicles/app/"

#define AliPaySchema @"alipaySchema"
#define AliPayRedirectURL @"changePaySuccess/"
#define AliPayPID @"2088021322429250"
#define AliPayProductID @"beituowulian@126.com"
#define AliPayPrivateKey @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKn6JFEMyXM4w3DmxCt8OSBbZFlEFzpvy5/nmmNCnrjmN1Xhpo6h/oRIJkClN8YTbnS0RM1BmZTdYZ9XJrCg2Q19EyRA8VTPyUJ+RHO0vcc0UFGSpBdVDC1B5eqRo6maaFNQSOE8C6oqfEfYSmTzGG5q8zrnUlvyyjEj82AiwxgJAgMBAAECgYBUA8C5kvFrky3++wERYVg3JxUljiNRDfr+sZI856veRmQpfdw+edF8ftDxePhlXitlYcj0Dy2jR/09re0AtrAxTVwdSuRH+nQSDBpcCb5T7+3AYmbcjwT20roCdiN+a0HyhwGw9vjWKgJFckJTot99P0meiNTeOpTQmLWgb0MhUQJBANL3x7gbTbEZNcDodaalu+8Z6cS99oSEKKEWRoyxxslwWazw2GuohaQWVBS/+Icpf6cJSqzz7ZVL8yv9HZU9fmUCQQDOQnG665ydMJA+Rt3hoQtGt28xPKpr6jKUDJYyJmmWTV4+LTVnstW52dJQe8vG5SvtjC3mJN+eZEmS6cWt51bVAkEAiorJj2/uv2SM1lhSGtQv7L8Zs2gEodWgUNkuD2sX3mV3noizt09+I9m98bp33Vs1R15kuduepDKySKr6aG1vuQJAbvfKblEymlM1V3wTBb/57BrmZKwJ2DNQ8Nvwtpx59BY93N+/usG5QNsSzqA72w8S2iOv0gEGmhXEi5jf9Jbg3QJAJxKZkcLXKEHFUPxMeNd+4keawTzVM+l7NdF0Rs2F5IdrT8YVtq4GE2yIm43zCwMX2S0hlXJRKtswLxVNq1OIZQ=="

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *priceTF;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 100, 40);
    [button setTitle:@"去支付" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(pay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}
- (void)pay
{
    [[AlipaySDK defaultService] payOrder:[self getAlipayOrderString] fromScheme:@"alipaySDK" callback:^(NSDictionary *resultDic) {
        NSLog(@"%@",resultDic);
    }];
}

- (NSString *)getAlipayOrderString
{
    NSString *price = self.priceTF.text;
    
    NSMutableString * orderSpec = [NSMutableString string];
    [orderSpec appendFormat:@"partner=\"%@\"", AliPayPID];
    
    [orderSpec appendFormat:@"&seller_id=\"%@\"", AliPayProductID];
    [orderSpec appendFormat:@"&out_trade_no=\"%@\"", [self generateTradeNO]];
    [orderSpec appendFormat:@"&subject=\"%@\"", @"砼车帮充值金额"];
    [orderSpec appendFormat:@"&total_fee=\"%@\"", [NSString stringWithFormat:@"%@", price]];
    [orderSpec appendFormat:@"&notify_url=\"%@\"", [NSString stringWithFormat:@"%@%@%@%ld.htm", kServerPath, kInterfacePath, AliPayRedirectURL]];
    [orderSpec appendFormat:@"&service=\"%@\"",@"mobile.securitypay.pay"];
    [orderSpec appendFormat:@"&payment_type=\"%@\"",@"1"];//1
    [orderSpec appendFormat:@"&_input_charset=\"%@\"",@"utf-8"];//utf-8
    [orderSpec appendFormat:@"&it_b_pay=\"%@\"",@"30m"];//30m
    [orderSpec appendFormat:@"&show_url=\"%@\"",@"m.alipay.com"];//m.alipay.com
    id<DataSigner> signer = CreateRSADataSigner(AliPayPrivateKey);
    NSString *signedString = [signer signString:orderSpec];
    if (signedString) {
        return [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                orderSpec, signedString, @"RSA"];

    } else {
        return nil;
    }
}

- (NSString *)generateTradeNO
{
    static int kNumber = 15;
    
    NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)time(0));
    for (int i = 0; i < kNumber; i++)
    {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
        
    }
    return resultStr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
